<template>
	<div class="system-company-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="450px"
			:close-on-click-modal="false">
            <el-form ref="companyDialogFormRef" :model="state.ruleForm" size="default" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" class="mb20">
						<el-form-item label="公司名">
							<el-input v-model="state.ruleForm.sys_name"  placeholder="请输入公司名" class="w100" />
						</el-form-item>
                        <el-form-item label="公司域名">
							<el-input v-model="state.ruleForm.website" placeholder="请输入公司域名" class="w100" />
						</el-form-item>

						<el-form-item label="首页Logo">
							<el-upload
								style="line-height: 1;"
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="file => onChange(file, 'home_logo')">
								<img v-if="state.ruleForm.home_logo" :src="VITE_IMG_URL + state.ruleForm.home_logo" class="my-imgage" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>

                        <el-form-item label="菜单Logo">
							<el-upload
								style="line-height: 1;"
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="file => onChange(file, 'sys_logo')">
								<img v-if="state.ruleForm.sys_logo" :src="VITE_IMG_URL + state.ruleForm.sys_logo" class="my-imgage" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>

                        <el-form-item label="菜单小Logo">
							<el-upload
								style="line-height: 1;"
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="file => onChange(file, 'logo_min')">
								<img v-if="state.ruleForm.logo_min" :src="VITE_IMG_URL + state.ruleForm.logo_min" class="my-imgage" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>
                        <el-form-item label="网站Icon">
							<el-upload
								style="line-height: 1;"
								:show-file-list="false"
								:auto-upload="false"
								accept= "image/jpeg, image/png, image/jpg"
								:on-change="file => onChange(file, 'ico')">
								<img v-if="state.ruleForm.ico" :src="VITE_IMG_URL + state.ruleForm.ico" class="my-imgage" />
								<div v-else class="my-upload-avatar">
									<SvgIcon name="ele-Plus" :size="20"/>
								</div>
							</el-upload>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemCompanyDialog">
import { reactive, ref } from 'vue';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
import { companyApi } from '/@/api/company/index';
import { commApi } from '/@/api/comm/index';
// 定义变量内容
const companyDialogFormRef = ref();
const VITE_IMG_URL = import.meta.env.VITE_IMG_URL
const state = reactive({
	ruleForm: {
		sys_name: '', 
        website:'',
        sys_logo:'',
        logo_min:'',
        ico:'',
	},
	dialog: {
		isShowDialog: false,
		title: '',
		submitTxt: '保存配置',
	},
    configRow: {},
});

// 打开弹窗
const openDialog = (row) => {
    state.configRow = row;
	state.dialog.title = `${row.title} 配置`;
	state.dialog.isShowDialog = true;
    state.ruleForm = {
		sys_name: '', 
        website:'',
        sys_logo:'',
        logo_min:'',
        ico:'',
	}
	getConfigs()
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
/**
 * 文件选择改变时触发
 */
const onChange = (file,name) => {
	if (file.status !== "ready") return;
	const formData = new FormData();
	formData.append('file', file.raw);
	commApi().upload(formData).then(res=>{
		state.ruleForm[name] = res.data;
	})
}
// 提交
const onSubmit = () => {
    companyApi().company_conf_edit(state.ruleForm,state.configRow.id).then(res => {
        closeDialog(); // 关闭弹窗
    })
};

const getConfigs = () => {
	companyApi().company_conf({id:state.configRow.id}).then(res=>{
        if(res.data){
            state.ruleForm = res.data
        }
	})
}

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style lang="scss" scoped>
:deep(.el-input-number .el-input__inner) {
	text-align: left;
}
.menu-data-tree {
	width: 100%;
	border: 1px solid var(--el-border-color);
	border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
	padding: 5px;
}
</style>
